package com.isunland.managesystem.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.android.volley.toolbox.ImageLoader;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.Knowledgebasemanagement;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/2/14 10:27
 */
public class KnowledgeBaseListAdapter<T> extends BaseAdapter {
  private ArrayList<T> objects = new ArrayList<>();
  private LayoutInflater layoutInflater;
  private final ImageLoader mImageLoader;

  public KnowledgeBaseListAdapter(Context context, ArrayList<T> objects) {
    this.layoutInflater = LayoutInflater.from(context);
    this.objects = objects;
    mImageLoader = RequestManager.getImageLoader();
  }

  @Override public int getCount() {
    return objects.size();
  }

  @Override public T getItem(int position) {
    return objects.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = layoutInflater.inflate(R.layout.adapter_knowledge_item, parent, false);
      convertView.setTag(new ViewHolder(convertView));
    }
    initializeViews(getItem(position), (ViewHolder) convertView.getTag());
    return convertView;
  }

  private void initializeViews(T object, ViewHolder holder) {
    Knowledgebasemanagement item = (Knowledgebasemanagement) object;

    holder.tvTitle.setText(item.getTitle());//标题
    holder.tvContent.setText(item.getDocKeyWords());//关键字
    holder.tvContent2.setVisibility(View.GONE);
    holder.tvBottomLeft.setText(item.getDocKindName());//类别
    holder.tvBottomRight.setText(
        MyDateUtil.format(MyDateUtil.parse(item.getRegDate()), MyDateUtil.TYPE_DATE));//登记时间

    //缩略图显示

    if (isOnePic(item)) {
      //一张图片时
      holder.ivPictureA.setVisibility(View.GONE);
      holder.ivPictureB.setVisibility(View.GONE);
      holder.ivPictureC.setVisibility(View.GONE);
      volleyPostGetPic(item.getThumbnail1(), holder.ivPicture);
    } else {
      //多张图时
      holder.ivPicture.setVisibility(View.GONE);
      volleyPostGetPic(item.getThumbnail1(), holder.ivPictureA);
      volleyPostGetPic(item.getThumbnail2(), holder.ivPictureB);
      volleyPostGetPic(item.getThumbnail3(), holder.ivPictureC);
    }
  }

  /*判断是否为一张图片*/
  private boolean isOnePic(Knowledgebasemanagement item) {
    if (!TextUtils.isEmpty(item.getThumbnail1())
        && TextUtils.isEmpty(item.getThumbnail2())
        && TextUtils.isEmpty(item.getThumbnail3())) {
      return true;
    }
    if (!TextUtils.isEmpty(item.getThumbnail2())
        && TextUtils.isEmpty(item.getThumbnail1())
        && TextUtils.isEmpty(item.getThumbnail3())) {
      return true;
    }
    if (!TextUtils.isEmpty(item.getThumbnail3())
        && TextUtils.isEmpty(item.getThumbnail1())
        && TextUtils.isEmpty(item.getThumbnail2())) {
      return true;
    }
    return false;
  }

  private void volleyPostGetPic(String pictureUrl, ImageView imageView) {
    if (!TextUtils.isEmpty(pictureUrl)) {
      imageView.setVisibility(View.VISIBLE);
      String urlImage = ApiConst.getDownLoadUrlByGet(pictureUrl);
      ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, // 控件名
          R.drawable.fail_pic, R.drawable.fail_pic); // 默认图片
      mImageLoader.get(urlImage, listener);
    } else {
      imageView.setVisibility(View.GONE);
    }
  }

  class ViewHolder {
    @BindView(R.id.tv_title) TextView tvTitle;
    @BindView(R.id.tv_content) TextView tvContent;
    @BindView(R.id.tv_content2) TextView tvContent2;
    @BindView(R.id.iv_picture) ImageView ivPicture;
    @BindView(R.id.iv_pictureA) ImageView ivPictureA;
    @BindView(R.id.iv_pictureB) ImageView ivPictureB;
    @BindView(R.id.iv_pictureC) ImageView ivPictureC;
    @BindView(R.id.tv_bottomLeft) TextView tvBottomLeft;
    @BindView(R.id.tv_bottomRight) TextView tvBottomRight;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
